package com.yonyou.brigade.order_center.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.order_center.model.api.BriFlightOrderInfoAPIDTO;
import com.yonyou.brigade.order_center.model.api.BriHotelOrderInfoAPIDTO;
import com.yonyou.brigade.order_center.service.IBriFlightOrderInfoAPIService;
import com.yonyou.brigade.order_center.service.IBriHotelOrderInfoAPIService;
import com.yonyou.brigade.order_center.service.IBriTrainOrderInfoAPIService;
import com.yonyou.brigade.util.resultutil.HttpSendUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2018/9/3.
 */
@Controller
@RequestMapping("/order_center/briFlightOrder")
public class BriTest {

    @Autowired
    @Qualifier("briFlightOrderInfoAPIServiceImpl")
    private IBriFlightOrderInfoAPIService briFlightOrderInfoAPIService;
    @Autowired
    @Qualifier("briHotelOrderInfoAPIServiceImpl")
    private IBriHotelOrderInfoAPIService briHotelOrderInfoAPIService;
    @Autowired
    @Qualifier("briTrainOrderInfoAPIServiceImpl")
    private IBriTrainOrderInfoAPIService briTrainOrderInfoAPIService;

    @Value("${ordercenter.url.projecturl}")
    private String url;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @RequestMapping(value = "/getBriFlightOrder",method = RequestMethod.POST)
    public @ResponseBody Map<String, String> testAddRtpnrPriURL(@RequestBody JSONObject body){

        Map<String,String> parmas = new HashMap<>();
        Date date = new Date();
        String endTs = sdf.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE,-1);
        date = calendar.getTime();
        String startTs = sdf.format(date);
        parmas.put("startTs",startTs);
        parmas.put("endTs",endTs);

        String nurl = "http://192.168.155.1:8080/ayUser/test";
        List<BriHotelOrderInfoAPIDTO> briHotelOrderInfoAPIDTO = briHotelOrderInfoAPIService.getBriHotelOrderInfoAPIDTOS(parmas);
//        List<BriFlightOrder> orders = briFlightOrderService.getBriFlightOrder(parmas);
//        List<BriFlightInfo> infos = briFlightInfoService.getBriFlightInfo(parmas);
//        List<BriFlightPassenger> passengers = briFlightPassengerService.getBriFlightPassenger(parmas);
//        System.out.println(list.size());
//        BriFlightOrderInfoAPIDTO bri = list.get(0);
//        BriFlightOrder bo = bri.getBriFlightOrder();
//        String status = bo.getAuthStatus();

        if(briHotelOrderInfoAPIDTO!=null&&briHotelOrderInfoAPIDTO.size()>0)
        {
            try {
                Map<String,Object> sendMap = new HashMap<>();
                sendMap.put("code","0");
                sendMap.put("BriHotelOrderInfoAPIDTO",briHotelOrderInfoAPIDTO);
                sendMap.put("information","操作成功！");
                System.out.println("sendMap====" + sendMap.toString());
                String jsonStr = JSONObject.toJSONString(sendMap);

                JSONObject object = JSONObject.parseObject(jsonStr);
                JSONArray jsonArray = object.getJSONArray("BriFlightOrderInfoAPIDTOs");
                JSONObject object1 = jsonArray.getJSONObject(0);
                JSONArray jsonArray1 = object1.getJSONArray("briFlightInfos");
                JSONObject object2 = jsonArray1.getJSONObject(0);
                Long arriveTime = (long) object2.get("arriveTime");
                long l = (long) arriveTime;
                System.out.println("arriveTime" + arriveTime);

                SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
                Date date1 = new Date(arriveTime);
                  String time1 = sdf1.format(date1);
                System.out.println("time" + time1);

                System.out.println("jsonStr===="+jsonStr);
                String results = HttpSendUtils.sendPost(url,jsonStr);
                System.out.println(results);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        Map<String,String> result = new HashMap<>();
        result.put("code","0");
        result.put("information","操作成功");
        return result;
    }

    @RequestMapping(value = "/getUcUser",method = RequestMethod.POST)
    public @ResponseBody Map<String, String> testUcUser(@RequestBody JSONObject body){
        Map<String,String> result = new HashMap<>();

        return result;
    }
}